<?php
/**
 * @description 电商仓库存处理类
 * @access public
 * @author xiayuanjun@shopex.cn
 * @ver 0.1
 */
class ome_store_manage_branch extends ome_store_manage_abstract implements ome_store_manage_interface{

    /**
     * 添加发货单节点的库存处理方法
     *
     * @param array $params 传入参数
     * @param string $err_msg 错误信息
     * @return boolean true/false
     */
    public function addDly($params, &$err_msg){
        $branchObj        = app::get('ome')->model("branch");
        $branchProductLib = kernel::single('ome_branch_product');
        $basicMStockFreezeLib = kernel::single('material_basic_material_stock_freeze');
        
        $delivBranch  = $branchObj->getDelivBranch($params['branch_id']);
        $branchIds    = $delivBranch[$params['branch_id']]['bind_conf'];
        $branchIds[]  = $params['branch_id'];
        
        //增加branch_product的冻结库存
        foreach($params['delivery_items'] as $key=>$items)
        {
            $sql = "SELECT sum( IF( store < store_freeze, 0, store - store_freeze ) ) AS 'has' FROM sdb_ome_branch_product 
                    WHERE product_id=".$items['product_id']." AND branch_id IN (".implode(',', $branchIds).") group by product_id";
            $branch_p = $branchObj->db->selectrow($sql);

            if (!is_numeric($items['number']))
            {
                $err_msg    .= $items['product_name'] .":请输入正确数量";
                return false;
            }

            if (empty($branch_p['has']) || $branch_p['has'] == 0 ||  $branch_p['has'] < $items['number'])
            {
                $err_msg    .= $items['product_name'] .":商品库存不足";
                return false;
            }

            //仓库货品预占冻结
            $branchProductLib->freez($params['branch_id'], $items['product_id'], $items['number']);

            //订单货品预占释放
            $basicMStockFreezeLib->unfreeze($items['product_id'], material_basic_material_stock_freeze::__ORDER, $params['order_id'], '', material_basic_material_stock_freeze::__SHARE_STORE, $items['number']);
        }
        
        return true;
    }

    /**
     * 取消发货单节点的库存处理方法
     *
     * @param array $params 传入参数
     * @param string $err_msg 错误信息
     * @return boolean true/false
     */
    public function cancelDly($params, &$err_msg){

        $branchProductLib    = kernel::single('ome_branch_product');
        $basicMStockFreezeLib = kernel::single('material_basic_material_stock_freeze');
        
        foreach($params['delivery_items'] as $key=>$dly_item)
        {
            $rs    = $branchProductLib->unfreez($params['branch_id'], $dly_item['product_id'], $dly_item['number']);
            if ($rs == false)
            {
                $err_msg    = '仓库货品冻结释放失败!';
                return false;
            }

            $rs = $basicMStockFreezeLib->freeze($dly_item['product_id'], material_basic_material_stock_freeze::__ORDER, $params['order_id'], $params['shop_id'], 0, material_basic_material_stock_freeze::__SHARE_STORE, $dly_item['number']);
            if ($rs == false) {
                $err_msg    = '订单货品预占添加失败!';
                return false;
            }
        }
        
        return true;
    }

    /**
     * 发货单发货节点的库存处理方法
     *
     * @param array $params 传入参数
     * @param string $err_msg 错误信息
     * @return boolean true/false
     */
    public function consignDly($params, &$err_msg){
    
        $branchProductLib      = kernel::single('ome_branch_product');
        $basicMStockLib = kernel::single('material_basic_material_stock');
        
        foreach($params['delivery_items'] as $key=>$dly_item)
        {
            $rs    = $branchProductLib->unfreez($params['branch_id'], $dly_item['product_id'], $dly_item['number']);
            if ($rs == false)
            {
                $err_msg    = '仓库货品冻结释放更新失败!';
                return false;
            }

            $rs    = $basicMStockLib->unfreeze($dly_item['product_id'], $dly_item['number']);
            if ($rs == false)
            {
                $err_msg    = '货品冻结释放更新失败!';
                return false;
            }
        }

        //电商仓发货，仓库货品实际库存扣减跟着出入库明细生成，所以不在这里处理
        
        return true;
    }

    /**
     * 订单暂停发货单取消节点的库存处理方法
     *
     * @param array $params 传入参数
     * @param string $err_msg 错误信息
     * @return boolean true/false
     */
    public function pauseOrd($params, &$err_msg){
    
        $branchProductLib    = kernel::single('ome_branch_product');
        $basicMStockFreezeLib = kernel::single('material_basic_material_stock_freeze');
        
        foreach($params['delivery_items'] as $key=>$dly_item)
        {
            $rs    = $branchProductLib->unfreez($params['branch_id'], $dly_item['product_id'], $dly_item['number']);
            if ($rs == false)
            {
                $err_msg    = '仓库货品冻结释放更新失败!';
                return false;
            }

            $rs = $basicMStockFreezeLib->freeze($dly_item['product_id'], material_basic_material_stock_freeze::__ORDER, $params['order_id'], $params['shop_id'], 0, material_basic_material_stock_freeze::__SHARE_STORE, $dly_item['number']);
            if ($rs == false) {
                $err_msg    = '订单货品冻结增加失败!';
                return false;
            }
        }
        
        return true;
    }

    /**
     * 订单恢复节点的库存处理方法
     *
     * @param array $params 传入参数
     * @param string $err_msg 错误信息
     * @return boolean true/false
     */
    public function renewOrd($params, &$err_msg){
        //订单暂停、第三方仓发货单直接走的是撤销，如果订单恢复后不需要相应的货品库存处理逻辑
    }
}